package com.showsu.editor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblUserExppackageMapper;
import com.showsu.editor.entity.po.TblUserExppackage;
import com.showsu.editor.entity.po.TblUserPrivilege;
import com.showsu.editor.enums.DeleteStatusEnum;
import com.showsu.editor.enums.StatusEnum;
import com.showsu.editor.enums.UserExpPackageTypeEnum;
import com.showsu.editor.service.IUserExppackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 17:36 2025/9/17
 * @Modified By:
 */
@Service
@Slf4j
public class UserExppackageServiceImpl  extends ServiceImpl<TblUserExppackageMapper, TblUserExppackage> implements IUserExppackageService {

    @Autowired
    private TblUserExppackageMapper mapper;

    @Override
    public TblUserExppackage queryUserExppackage(Long userId, UserExpPackageTypeEnum expType) {
        if (userId == null || expType == null) {
            return null;
        }
        return mapper.selectOne(new QueryWrapper<TblUserExppackage>().lambda()
                .eq(TblUserExppackage::getFlag, DeleteStatusEnum.Normal.getCode())
                .eq(TblUserExppackage::getStatus, StatusEnum.Enable.getCode())
                .eq(TblUserExppackage::getUserId, userId)
                .eq(TblUserExppackage::getExpType, expType.getCode())
                .gt(TblUserExppackage::getExpEndTime, new Date())
                .last("limit 1"));
    }

    @Override
    public Boolean addUserExppackage(Long userId, UserExpPackageTypeEnum expType, Integer times, Date vipEndTime, String orderTradeNo) {
        TblUserExppackage existedRecord = this.queryUserExppackage(userId, expType);
        if (existedRecord == null) {
            TblUserExppackage newRecord = new TblUserExppackage();
            newRecord.setUserId(userId);
            newRecord.setExpType(expType.getCode());
            newRecord.setExpCount(times);
            newRecord.setExpEndTime(vipEndTime);
            newRecord.setFreezeCount(0);
            newRecord.setOrderTradeNo(orderTradeNo);
            return this.save(newRecord);
        } else {
            existedRecord.setOrderTradeNo(orderTradeNo);
            existedRecord.setExpEndTime(vipEndTime);
            existedRecord.setExpCount(existedRecord.getExpCount() + times);
            return this.updateById(existedRecord);
        }
    }

    @Override
    public List<TblUserExppackage> queryExpiredUserExppackage() {
        return mapper.selectList(new QueryWrapper<TblUserExppackage>().lambda()
                .eq(TblUserExppackage::getFlag, DeleteStatusEnum.Normal.getCode())
                .eq(TblUserExppackage::getStatus, StatusEnum.Enable.getCode())
                .le(TblUserExppackage::getExpEndTime, new Date()));
    }

    @Override
    public Boolean freezeUserAvailableTimes(Long userId, UserExpPackageTypeEnum expType, Integer times) {
        Boolean result = false;
        if (userId == null || expType == null || times == null) {
            return result;
        }
        TblUserExppackage userExppackage = this.queryUserExppackage(userId, expType);
        if (userExppackage == null || userExppackage.getExpCount() == null
                || userExppackage.getExpCount().intValue() < times.intValue()) {
            return result;
        }
        UpdateWrapper<TblUserExppackage> updateWrapper = new UpdateWrapper();
        updateWrapper.setSql("exp_count = exp_count - " + times + ", freeze_count = freeze_count + " + times);
        updateWrapper.eq("id", userExppackage.getId());
        this.update(updateWrapper);
        return true;
    }

    @Override
    public Boolean reduceFreezeTimes(Long userId, UserExpPackageTypeEnum expType, Integer times) {
        Boolean result = false;
        if (userId == null || expType == null || times == null) {
            return result;
        }
        TblUserExppackage userExppackage = this.queryUserExppackage(userId, expType);
        if (userExppackage == null || userExppackage.getFreezeCount() == null
                || userExppackage.getFreezeCount().intValue() < times.intValue()) {
            return result;
        }
        UpdateWrapper<TblUserExppackage> updateWrapper = new UpdateWrapper();
        updateWrapper.setSql("freeze_count = freeze_count - " + times);
        updateWrapper.eq("id", userExppackage.getId());
        this.update(updateWrapper);
        return true;
    }

    @Override
    public Boolean returnFreezeTimes(Long userId, UserExpPackageTypeEnum expType, Integer times) {
        Boolean result = false;
        if (userId == null || expType == null || times == null) {
            return result;
        }
        TblUserExppackage userExppackage = this.queryUserExppackage(userId, expType);
        if (userExppackage == null || userExppackage.getFreezeCount() == null
                || userExppackage.getFreezeCount().intValue() < times.intValue()) {
            return result;
        }
        UpdateWrapper<TblUserExppackage> updateWrapper = new UpdateWrapper();
        updateWrapper.setSql("exp_count = exp_count + " + times + ", freeze_count = freeze_count - " + times);
        updateWrapper.eq("id", userExppackage.getId());
        this.update(updateWrapper);
        return true;
    }

    @Override
    public int exipreUserExppackage(Long id) {
        if (id == null) {
            return 0;
        }
        LambdaUpdateWrapper<TblUserExppackage> updateWrapper = new UpdateWrapper<TblUserExppackage>().lambda()
                .eq(TblUserExppackage::getId, id)
                .eq(TblUserExppackage::getFlag, DeleteStatusEnum.Normal.getCode());
        TblUserExppackage userExppackage = new TblUserExppackage();
        userExppackage.setStatus(StatusEnum.Disable.getCode());
        return mapper.update(userExppackage, updateWrapper);
    }
}
